----------------------------------------------------------------------------------------------------------------------------------
--[[

    晾晒模块

    self.drying_data = {
        harvest = "prefab_or_index", -- 收获物 的函数。
        userid = "userid", -- 用户id    --- 特定目标可用
        accept = {  -- 可接受的物品 + 文本
            ["prefab"] = STRINGS.ACTIONS.ACTIVATE.ACCOMPLISH,
        },
    },

]]--
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.hutao_com_drying_module or self.inst.replica._.hutao_com_drying_module
    end
    local function sync_fn(inst,self)
        local replica = GetReplica(self)
        if replica then
            self.drying_data.sync_flag = not self.drying_data.sync_flag
            replica:SetData(self.drying_data)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
--- 快速响应库
    local drying_buildings = {}
    local function get_linkings(userid)
        local need_to_clear_flag = false
        local linkings = {}
        for tempInst, com in pairs(drying_buildings) do
            if tempInst:IsValid() then
                if com:IsOnlyBelong(userid) then
                    table.insert(linkings, tempInst)
                end
            else
                need_to_clear_flag = true
            end
        end
        if need_to_clear_flag then
            local new_buildings = {}
            for tempInst, com in pairs(drying_buildings) do
                if tempInst:IsValid() then
                    new_buildings[tempInst] = com
                end
            end
            drying_buildings = new_buildings
        end
        return linkings
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_drying_module = Class(function(self, inst)
    self.inst = inst
    --------------------------------------------------------
    --- 通用数据、函数表
        self.DataTable = {}
        self.TempTable = {}
        self._onload_fns = {}
        self._onsave_fns = {}
    --------------------------------------------------------
    --- 干燥数据表
        self.drying_data = {}
        self:AddOnSaveFn(function()
            self:Set("drying_data", self.drying_data)
        end)
        self:AddOnLoadFn(function()
            self.drying_data = self:Get("drying_data", {})
        end)
    --------------------------------------------------------
    --- 收获函数
        self.harvest_fns = {}
    --------------------------------------------------------
    --- 同步数据
        inst:DoPeriodicTask(1,sync_fn,0,self)
    --------------------------------------------------------
    ---
        drying_buildings[inst] = self
    --------------------------------------------------------
end,
nil,
{
    
})
------------------------------------------------------------------------------------------------------------------------------
--- 接受物品.
    function hutao_com_drying_module:GetAcceptting()
        return self.drying_data.accept or {}
    end
    function hutao_com_drying_module:SetAcceptting(accept)
        self.drying_data.accept = accept or {}
        self:SyncData()
    end
    function hutao_com_drying_module:AddAccept(prefab,action_str)
        local temp_data = self:GetAcceptting()
        temp_data[prefab] = action_str or STRINGS.ACTIONS.ADDCOMPOSTABLE
        self:SetAcceptting(temp_data)
    end
    function hutao_com_drying_module:RemoveAccept(prefab)
        local temp_data = self:GetAcceptting()
        local new_table = {}
        for k, v in pairs(temp_data) do
            if k ~= prefab then
                new_table[k] = v
            end
        end
        self:SetAcceptting(new_table)
    end
    function hutao_com_drying_module:HasAccept(prefab)
        return self:GetAcceptting()[prefab] ~= nil
    end
    function hutao_com_drying_module:ClearAccept()
        self:SetAcceptting({})
    end
------------------------------------------------------------------------------------------------------------------------------
---
    function hutao_com_drying_module:GetCurrent()
        return self.drying_data.harvest
    end
------------------------------------------------------------------------------------------------------------------------------
--- 物品收获。
    function hutao_com_drying_module:SetHarvest(prefab_or_index)
        self.drying_data.harvest = prefab_or_index
        self:SyncData()
    end
    function hutao_com_drying_module:SetHarvestFn(prefab_or_index,fn)
        self.harvest_fns[prefab_or_index] = fn
    end
    function hutao_com_drying_module:SetHarvester(player_or_userid)
        if type(player_or_userid) == "table" and player_or_userid.userid then
            self.drying_data.userid = player_or_userid.userid
        elseif type(player_or_userid) == "string" then
            self.drying_data.userid = player_or_userid
        else
            self.drying_data.userid = nil
        end
        self:SyncData()
    end
    function hutao_com_drying_module:CanHarvest(player)
        if self.drying_data.userid == nil then
            return true
        end
        return self.drying_data.userid == player.userid
    end
    function hutao_com_drying_module:IsOnlyBelong(userid)
        return self.drying_data.userid == userid
    end
    function hutao_com_drying_module:DoHarvest(player)
        if not self:CanHarvest(player) then
            return
        end
        local harvest_index = self.drying_data.harvest
        local fn = self.harvest_fns[harvest_index]
        if fn then
            fn(self.inst,player)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- 同步
    function hutao_com_drying_module:SyncData()
        if self.__sync_task then
            self.__sync_task:Cancel()
        end
        self.__sync_task = self.inst:DoTaskInTime(0,sync_fn,self)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 数据绑定扫描.方便玩家调用扫描
    function hutao_com_drying_module:GetLinkingBuildings(player)
        return get_linkings(player.userid) or {}
    end
------------------------------------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_drying_module:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_drying_module:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_drying_module:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_drying_module:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_drying_module:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_drying_module:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_drying_module:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_drying_module:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_drying_module:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_drying_module







